<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );

function theme_options_init(){
	register_setting( 'sample_options', 'sncmono_theme_options', 'theme_options_validate' );
}

function theme_options_add_page() {
	add_theme_page( __( 'Theme Options', 'snc-mono' ), __( 'Theme Options', 'snc-mono' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}

function theme_options_do_page() {
	global $select_options, $radio_options;

	if ( ! isset( $_REQUEST['settings-updated'] ) )
		$_REQUEST['settings-updated'] = false;

	?>
	<div class="wrap">
		<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'snc-mono' ) . "</h2>"; ?>

		<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
		<div class="updated fade"><p><strong><?php _e( 'Options saved', 'snc-mono' ); ?></strong></p></div>
		<?php endif; ?>

		<form method="post" action="options.php">
			<?php settings_fields( 'sample_options' ); ?>
			<?php $options = get_option( 'sncmono_theme_options' ); ?>
			<?php if (!isset($options['credit'])) { $options['credit'] = 0; } ?>

			<table class="form-table">
				<tr valign="top"><th scope="row"><?php _e( 'Header background color', 'snc-mono' ); ?></th>
					<td>
						<input id="sncmono_theme_options[header_color]" class="regular-text" type="text" name="sncmono_theme_options[header_color]" value="<?php esc_attr_e( $options['header_color'] ); ?>" />
						<label class="description" for="sncmono_theme_options[header_color]"><?php _e( 'e.g. #444444 (it\'s a name for kind of gray color in hex)', 'snc-mono' ); ?></label>
					</td>
				</tr>
				<tr valign="top"><th scope="row"><?php _e( 'Credit', 'snc-mono' ); ?></th>
					<td>
						<input id="sncmono_theme_options[credit]" name="sncmono_theme_options[credit]" type="checkbox" value="1" <?php checked( '1', $options['credit'] ); ?> />
						<label class="description" for="sncmono_theme_options[credit]"><?php _e( 'Check to show credit for this theme in footer.', 'snc-mono' ); ?></label>
					</td>
				</tr>
			</table>

			<p class="submit">
				<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'snc-mono' ); ?>" />
			</p>
		</form>
	</div>
	<?php
}

function theme_options_validate( $input ) {
	global $select_options, $radio_options;
	
	if ( ! isset( $input['credit'] ) )
		$input['credit'] = null;
		$input['credit'] = ( $input['credit'] == 1 ? 1 : 0 );

	$input['header_color'] = wp_filter_nohtml_kses( $input['header_color'] );

	return $input;
}